///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.data.vo {
	import mx.utils.ObjectUtil;
	
	import sugarcookie.data.vo.Img;
	import sugarcookie.data.vo.Paragraph;
	
	/**
	 * Value Object used when parsing XHTML.
	 */
	public class Div extends Object {
		private var _xml:XML;
		private var _id:String;
		private var _title:String;
		private var _style:String;
		private var _divs:Array = [];
		private var _index:uint;
		private var _paragraphs:Array = [];
		private var _images:Array = [];
		
		public function Div(_div:XML,_i:uint=0) {
			_index = _i;
			_xml = _div;
			
			if (_xml.attribute("id")!=undefined) _id = _xml.@id;
			if (_xml.attribute("title")!=undefined) _title = _xml.@title;
			if (_xml.attribute("style")!=undefined) _style = String(_xml.@style).indexOf("array")!=-1?"array":null;
			
			var i:int;
			var dList:XMLList = _xml.div;
			for (i=0;i<dList.length();i++) {
				var _d:Div = new Div(dList[i],i);
				addDiv(_d);
			}
			
			for (i=0;i<_xml.p.length();i++) {
				var _p:Paragraph = new Paragraph(_xml.p[i],i);
				addParagraph(_p);
			}
			
			var iList:XMLList = _xml.img;
        	for (i=0;i<iList.length();i++) {
        		var _img:Img = new Img(iList[i]);
        		addImage(_img);
        	}
		}
		
		private function addDiv(_d:Div):void {
			if (_style=="array") _divs.push(_d);
			else _divs[_d.title] = _d;
		}
		
		private function addParagraph(_p:Paragraph):void {
			if (_style=="array") _paragraphs.push(_p);
			else _paragraphs[_p.title] = _p;
		}
		
		private function addImage(_img:Img):void {
			if (_style=="array") _images.push(_img);
			else _images[_img.title] = _img;
		}
		
		public function get xml():XML {
			return _xml;
		}
		
		public function get index():uint {
			return _index;
		}
		
		public function get id():String {
			return _id;
		}
		
		public function get title():String {
			return _title;
		}
		
		public function get divs():Array {
			return _divs;
		}
		
		public function get paragraphs():Array {
			return _paragraphs;
		}
		
		public function get images():Array {
			return _images;
		}
		
		public function toString():String {
			return ObjectUtil.toString(this);
		}
	}
}